package com.example.concurrentprogramming8016.future.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author liyanhao
 * @Date 2023-01-08 21:42
 * allOf方法用来实现多CompletableFuture的同时返回。
 */
public class AllOfDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("A");
            return "hello ";
        });
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("B");
            return "world ";
        });
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("C");
            return "java ";
        });
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("D");
            return "world";
        });
        CompletableFuture<Void> completableFuture = CompletableFuture.allOf(future, future1, future2, future3);
        System.out.println(future.get() + future1.get() + future2.get() + future3.get());
        System.out.println(future.isDone() + "" + future1.isDone() + future2.isDone() + future3.isDone());

    }
}
